package org.cytoscape.cytargetlinker.internal.io;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.cytoscape.cytargetlinker.internal.model.Edge;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.ParserAdapter;

public class XgmmlParser {
	
	private List<Edge> edges;
	private String networkName;
	private Map<String, String> networkProperties;
	
	public XgmmlParser() {
		edges = new ArrayList<Edge>();
		networkProperties = new HashMap<String, String>();
	}
	
	public void parseXgmmlFile(File file, List<String> ids) throws Exception {
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = spf.newSAXParser();
		ParserAdapter pa = new ParserAdapter(sp.getParser());
		CyTargetLinkerParser handler = new CyTargetLinkerParser(ids);
		pa.setContentHandler(handler);
		pa.parse(new InputSource(new FileInputStream(file)));
		
		networkName = handler.getNetworkName();
		networkProperties = handler.getNetworkProperties();
		edges = handler.getEdgeList();
	
		ids.clear();
	}
	
	public List<Edge> getEdges() {
		return edges;
	}

	public String getNetworkName() {
		return networkName;
	}

	public Map<String, String> getNetworkProperties() {
		return networkProperties;
	}
}
